<?php 	$hostname = Configure::read('Sfp.hostname'); ?>

<h2>Share event <em><?php echo $event['name']; ?></em></h2><br />
<h3>Event host: <?php echo $familyHost['name']; ?></h3>


<script type="text/javascript">
	$j(function() {			
		var familyHost = "<?php echo $familyHost['name']; ?>";

		// family names
		var availableTags = [
			<?php foreach ($all_families as $family): 
					if ($family != $familyHost['name']) echo '"'.$family.'",';
				  endforeach;
			?>
		];

		// family ids
		var correspondingIDs = [
			<?php foreach ($all_families as $id => $family): 
					if ($id != $familyHost['id']) echo $id.', ';
				  endforeach;
			?>
		];

		$j( "#tags" ).autocomplete({
			source: availableTags,
			delay: 100,
			minLength: 2
		});

		$j( "#share_add_button" ).button({ 
			label: 'Add family',
			disabled: true
		});

		$j('#share_save').button({
			label: 'Share event',
		});

		$j('#tags').change(function() {
			if ($j('#tags').val().trim() != '') { 
				$j("#share_add_button").button('enable'); 
			}
			else {
				$j('#share_add_button').button('disable');
			}
		});

		$j( "#share_add_button" ).click(function() {
			// check for valid family name
			if ($j('#tags').val().trim() == familyHost) {
				alert(familyHost + ' is already hosting this event.');
				return false;
			}
			else if ($j.inArray($j('#tags').val().trim(), availableTags) == -1) {
				alert('invalid family name. choose one from the list');				
				return false;
			}
			
			// check for duplicates

			var duplicate = false;		
			var duplicate_pos = -1;	

			$j('.share_with_families_entry').each(function(index) {
				if ($j(this).text().trim() == $j('#tags').val().trim()) { 
					duplicate = true; 
					duplicate_pos = index + 1;
					return false; 
				}
			});

			if (!duplicate) { 
				$j('#share_with_families_ol').append('<li class="share_with_families_li">' + 
													 '<a class="share_with_families_remove_family" href="#">remove</a>' + 
													 '<div class="share_with_families_entry">' + 
													 $j('#tags').val().trim() + 
													 '</div>' +
													 '</li>'); 
				initializeRemoveLinks();

				// add hidden field to submit form
				$j('#share_save_form').append('<input type="hidden" name="' + $j('#tags').val().trim() +
																 '" value="' + 
							correspondingIDs[$j.inArray($j('#tags').val().trim(), availableTags)] + '" /\>');
			}
			else alert('Family ' + $j('#tags').val().trim() + ' is already in the list at number ' + duplicate_pos);

			// clear the input
			$j('#tags').val('');
		});

		/*
		$j('#share_save').click(function() {
			var families = $j('.share_with_families_entry').toArray();
			
			var post_data = '';

			for (i = 0; i < families.length; i++) {
				//alert(i + ' ' + (families[i]).innerHTML.trim());
				post_data += (families[i]).innerHTML.trim() + ',';
			}

			$j.ajax({
			  type: "POST",
			  url: "<?php echo $hostname; ?>/events/updatesharing",
			  data: "families=" + post_data,
			  success: function(){
				//$(this).addClass("done");
				//$j('#share_with_families_wrapper').append(
				alert('done');
				window.location.href = '<?php echo $hostname; ?>events/share/event:1';
			  }
			});
		});
		*/

		function initializeRemoveLinks() {
			$j('.share_with_families_remove_family').each(function() {
				$j(this).click(function() {
					// remove the li element 
					$j(this).parent().remove();

					// remove the corresponding form hidden field
					$j('input[name="' + $j(this).siblings().text().trim() + '"]').remove();
					
					// prevent following the link
					return false;
				});
			});
		}

		initializeRemoveLinks();
	});
</script>



<div id="share_wrapper"> <!-- BEGIN MAIN WRAPPER -->

<div id="share_with_families_wrapper"> <!-- BEGIN WRAPPER -->
	<p>Share this event with the following families:</p>

	<div id="share_with_families_container">
		<ol id="share_with_families_ol"><?php 
		$i = 0;
		foreach ($familiesSharingEvent as $family): 
			if ($family != $familyHost['name']) { ?>
			<li class="share_with_families_li">
				<a class="share_with_families_remove_family" href="#">remove</a>
				<div class="share_with_families_entry">
					<?php echo $family; ?>
				</div>
			</li><?php
			$i++;
			}			
		endforeach; ?>
		</ol>
	</div>

</div> <!-- END WRAPPER -->


<div id="share_with_families_add_wrapper"> <!-- BEGIN WRAPPER -->
	<div id="share_with_families_add_container">
		<label for="tags">Search and add families:</label><br />
		<input id="tags">
		<input id="share_add_button" type="submit" />
	</div>

</div> <!-- END WRAPPER -->


<div>
	<form id="share_save_form" action="<?php echo $this->Html->url('/events/updatesharing') . 
										'/event:' . $event['id']; ?>" method="POST">
		<input id="share_save" type="submit" />
		<?php
		foreach ($familiesSharingEvent as $id => $family): 
			if ($family != $familyHost['name']) { ?>
				<input type="hidden" name="<?php echo $family; ?>" value="<?php echo $id; ?>" /><?php
			}			
		endforeach; ?>
	</form>
</div>

</div> <!-- END MAIN WRAPPER -->
